main(n,p)
{
	long r;
	for(gets(&n);~scanf("%d%d",&n,&p);printf("%ld\n",r))
		for(r=p;n-->1;r%=1000000007)
			r*=p-1;
}
